package com.kingstar.ceres.utils;

import com.kingstar.ceres.constants.CommonConstants;
import imix.ConfigError;
import imix.DataDictionary;
import imix.imix10.Message;

public class ParseMsgUtil {

//    public static final String BEGIN_STR=" 35=";
//    public static final String END_STR=" ";
    public static final String BEGIN_STR="\u000135=";
    public static final String END_STR="\u0001";
    public static DataDictionary DICTIONARY=null;
    static {
        try {
            DICTIONARY=new DataDictionary(CommonConstants.IMIX_DICTIONARY_XML_10);
        } catch (ConfigError e) {
            e.printStackTrace();
        }
    }
    public static <T extends Message> T parseStrToMessage(String msg){
        T message=null;
        try {
            for (int i = 0; i < CommonConstants.msgType.length; i++) {
                if (msg.contains(BEGIN_STR+CommonConstants.msgType[i]+END_STR)){
                    message=(T)CommonConstants.clazz[i].newInstance();
                    message.fromString(msg,DICTIONARY,true);
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return message;

    }
}
